package dahuiji.common.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.sun.org.apache.bcel.internal.generic.LSTORE;

import dahuiji.common.core.DateCaozuo;
import dahuiji.common.dao.BaceDao;
import dahuiji.common.entity.Line;
import dahuiji.common.entity.LineRecord;
import dahuiji.common.sysdata.CommonData;

public class InitDataMemory {
	private static BaceDao baceDao;
	private static DateCaozuo dateCaozuo;

	public void getAllLineTOMemory() throws ParseException {

	}

	/**
	 * 得到所有航线的记录
	 * 
	 * @return
	 */
	public static List<LineRecord> getAllLineRecord() {
		List<LineRecord> listRecords = new LinkedList<LineRecord>();// 之所以是LinkedList，因为后面要进行平凡的删除
		List list = null;
		String sql = "select r.ID,r.LINE_ID,r.DATE,r.PRICE,r.LINEDATE from tb_dahuiji_linerecord r,tb_dahuiji_line l where l.STATUS= "
				+ Constants.LINE_RIGHT_YES + " and r.LINE_ID=l.LINE_ID";
		baceDao = new BaceDao();
		list = baceDao.executeQuerySql(sql);
		LineRecord lineR = null;
		Object[] o;
		int length = list.size();
		dateCaozuo = new DateCaozuo();

		for (int i = 0; i < length; i++) {
			o = (Object[]) list.get(i);
			lineR = new LineRecord();
			lineR.setId(Long.valueOf(o[0].toString()));
			lineR.setLindId(Long.valueOf(o[1].toString()));
			try {
				lineR.setDate(dateCaozuo.toDate(o[2].toString()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			lineR.setPrice(Short.valueOf(o[3].toString()));
			try {
				if (o[4] != null) {
					lineR.setLineDate(dateCaozuo.toDate(o[4].toString()));
				}

			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			listRecords.add(lineR);
		}

		return listRecords;
	}

	/**
	 * 得到所有航线
	 * 
	 * @return
	 */
	public static List<Line> getAllLine() {
		List<Line> linesList = new ArrayList<Line>();
		baceDao = new BaceDao();
		List lines = null;
		DateCaozuo d = new DateCaozuo();
		String dateNoew = d.getNowDateString();
		String sql = "SELECT * from tb_dahuiji_line l where l.FDATE>=" + "'"
				+ dateNoew + "'" + " and l.STATUS=" + Constants.LINE_RIGHT_YES;
		lines = baceDao.executeQuerySql(sql);
		int length = lines.size();
		Line line = null;
		Object[] o;
		dateCaozuo = new DateCaozuo();
		for (int i = 0; i < length; i++) {
			try {
				o = (Object[]) lines.get(i);
				line = new Line();
				line.setId(Long.valueOf(o[0].toString()));
				line.setUserId(Long.valueOf(o[1].toString()));
				line.setFromCity(o[2].toString());
				line.setToCity(o[3].toString());
				line.setPrice(Short.valueOf(o[4].toString()));
				line.setfDate(dateCaozuo.toDate(o[5].toString()));
				if (o[6] != null) {// 如果是具体时间测没有
					line.setToDate(dateCaozuo.toDate(o[6].toString()));
				}
				line.setStatus(Short.valueOf(o[7].toString()));
				line.setTpye(Short.valueOf(o[8].toString()));
				line.setFlag(Short.valueOf(o[9].toString()));
				linesList.add(line);
				CommonData.allLine.put(line.getId(), line);// 加到内存

			} catch (Exception e) {
				System.out.println(e);
			}

		}
		return linesList;
	}

	/**
	 * 把记录和航线绑定放到内存里
	 */
	public static void initdate() {
		List<Line> lines = getAllLine();// 得到所有航线
		List<LineRecord> lineRecords = getAllLineRecord();// 得到所有记录
		int lLine = lines.size();
		int lLineRe = lineRecords.size();
		List<LineRecord> lineRecords2 = null;// 用来在Map<Long,List<LineRecord>> //
		// allLineRecord中添加
		// 把记录和航线对应
		for (int i = 0; i < lLine; i++) {
			lineRecords2 = new ArrayList<LineRecord>();
			for (int j = 0; j < lLineRe; j++) {
				if (lines.get(i).getId() == lineRecords.get(j).getLindId()) {
					lineRecords2.add(lineRecords.get(j));
				}
			}
			CommonData.allLineRecord.put(lines.get(i).getId(), lineRecords2);
			
		}
	}

	/**
	 * 清空内存数据
	 */
	public static void cleanData() {
		CommonData.allLine.clear();
		CommonData.allLineRecord.clear();
	}

}
